﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using EHTWebsite.Code;

namespace EHTWebsite.Code
{
    public class BannerProvider
    {
        /// <summary>
        /// Author : toanvt
        /// CreateDate : 04/02/2012
        /// Description : Lấy danh sách Banner cho phần quản trị -> trả về danh sách Banners
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public static DataSet GetBannersForBackEnd(Int32 phanVungID, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetBannersForBackEnd";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@PhanVungID", phanVungID));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        /// <summary>
        /// Author : toanvt
        /// CreateDate : 04/02/2012
        /// Description : Lấy danh sách Banner cho phần front end -> trả về danh sách Banner
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public static DataSet GetBannersForFrontEnd(Int32 phanVungID, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetBannersForFrontEnd";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@PhanVungID", phanVungID));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        /// <summary>
        /// Author : toanvt
        /// CreatedDate : 06/12/2012
        /// Description : Insert, Update Banner
        /// </summary>
        /// <param name="theBanner"></param>
        /// <param name="IsAdd"></param>
        /// <returns></returns>
        public static Int32 IUBanner(Banner theBanner, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUBanner";
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter theBannerIDParameter = new SqlParameter();

            theBannerIDParameter.ParameterName = "@BannerIDOut";
            theBannerIDParameter.Direction = ParameterDirection.Output;
            theBannerIDParameter.DbType = DbType.Int32;

            command.Parameters.Add(theBannerIDParameter);
            command.Parameters.Add(new SqlParameter("@BannerID", theBanner.BannerID));
            command.Parameters.Add(new SqlParameter("@BannerUrl", theBanner.BannerUrl));
            command.Parameters.Add(new SqlParameter("@PhanVungID", theBanner.PhanVungID));
            command.Parameters.Add(new SqlParameter("@GhiChuVN", theBanner.GhiChuVN));
            command.Parameters.Add(new SqlParameter("@GhiChuOther", theBanner.GhiChuOther));
            command.Parameters.Add(new SqlParameter("@Publish", theBanner.Publish));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theBanner.NumberOfOrder));
            command.Parameters.Add(new SqlParameter("@Url", theBanner.Url));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@BannerIDOut"].Value.ToString());
        }

        /// <summary>
        /// Author : toanvt
        /// CreatedDate : 06/12/2012
        /// Description : Xóa Banner
        /// </summary>
        /// <param name="theBanner"></param>
        /// <param name="IsAdd"></param>
        /// <param name="bannerID"></param>
        public static void DeleteBannerByBannerID(Int32 bannerID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteBannerByBannerID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@BannerID", bannerID));

            DataProvider.ExecuteNoneQuery(command);
        }

        public static Banner GetBannerByBannerID(Int32 bannerID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetBannerByBannerID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@BannerID", bannerID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            Banner theBanner = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theBanner = new Banner();
                theBanner.BannerID = Int32.Parse(ds.Tables[0].Rows[0]["BannerID"].ToString());
                theBanner.PhanVungID = Int32.Parse(ds.Tables[0].Rows[0]["PhanVungID"].ToString());
                theBanner.BannerUrl = ds.Tables[0].Rows[0]["BannerUrl"].ToString();
                theBanner.GhiChuOther = ds.Tables[0].Rows[0]["GhiChuOther"].ToString();
                theBanner.GhiChuVN = ds.Tables[0].Rows[0]["GhiChuVN"].ToString();
                theBanner.Publish = Boolean.Parse(ds.Tables[0].Rows[0]["Publish"].ToString());
                theBanner.NumberOfOrder = Int32.Parse(ds.Tables[0].Rows[0]["NumberOfOrder"].ToString());
                theBanner.Url = ds.Tables[0].Rows[0]["Url"].ToString();
            }

            return theBanner;
        }

        public static void ChangePublishByBannerID(Int32 bannerID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_ChangePublishByBannerID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@BannerID", bannerID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void UpdateNumberOfOrderByBannerID(Int32 bannerID, Int32 numberOfOrder)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateNumberOfOrderByBannerID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@BannerID", bannerID));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", numberOfOrder));
            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author : toanvt
        /// CreatedDate : 06/02/2012
        /// Description : Lấy danh phân vùng banner
        /// </summary>
        /// <returns></returns>
        public static DataSet GetPhanVungBanners()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetPhanVungBanners";
            command.CommandType = CommandType.StoredProcedure;

            DataSet ds = DataProvider.ExecuteDataSet(command);
            return ds;
        }
    }
}